// @flow
import React, { Component } from 'react'
import styles from './TaskList.css'
import { Button, notification } from 'antd'
import moment from 'moment'
import TaskListGrid from './TaskListGrid'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import TaskToolBar from 'components/TaskToolBar'

type Props = {
  history: Object
}
type States = {
  tasks: Array<Object>
}
class TaskList extends Component {
  props: Props
  state: States
  getTasks: Function
  addTask: Function
  resetData: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      tasks: []
    }
    this.getTasks = this.getTasks.bind(this)
    this.addTask = this.addTask.bind(this)
    this.resetData = this.resetData.bind(this)
  }

  componentDidMount () {
    this.getTasks()
  }
  resetData (data: Object) {
    // const { tasks } = this.state
    // const newTasks = tasks.map(item => {
    //   if (item === data) {
    //     item.model.jobCount = item.model.jobCount + 1
    //   }
    //   return item
    // })
    // this.setState({ tasks: newTasks })
    this.getTasks()
  }
  getTasks (fillterData: string) {
    let fillterQuery = ''
    if (fillterData) {
      fillterQuery = fillterData
    }
    universalFetch(`${__SERVICE_API__}analysis/tasks${fillterQuery}`)
      .then(res => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          notification.error({
            message: '提醒',
            description: json.message,
            placement: 'topRight',
            top: 63
          })
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        this.setState({
          tasks: json.result.map((d, i) => {
            d['startDate'] = moment.utc(d.startDate, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
            d['endDate'] = moment.utc(d.endDate, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
            return d
          })
        })
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
      })
  }

  addTask () {
    this.props.history.push('/analysis/task-add')
  }
  render () {
    const { tasks } = this.state
    return (
      <div className={styles['main']}>
        <div className={styles['action']}>
          <Button type='primary' style={{ marginLeft: '0', marginBottom: '0' }}
            onClick={this.addTask}>
            添加任务
          </Button>
          <div className={styles['taskToolBarWraper']}>
            <TaskToolBar tasks={tasks} getTasks={(data) => { this.getTasks(data) }} />
          </div>
        </div>
        <TaskListGrid {...{
          gridOptions:{
            localeText: {
              loadingOoo: '数据加载中...',
              noRowsToShow: '暂无数据',
              filterOoo: '搜索当前页面',
              equals: '等于',
              notEqual: '不等于',
              contains: '包含',
              startsWith: '开始于',
              endsWith: '结束于',
              copy: '复制',
              paste: '粘贴',
              copyWithHeaders: '带表头复制',
              toolPanel: '工具栏',
              export: '导出',
              csvExport: '导出CSV',
              excelExport: '导出Excel'
            },
            enableSorting: true,
            enableFilter: true
          },
          refreshTaskList: this.resetData,
          rowData: tasks,
          history: this.props.history
        }} />
      </div>
    )
  }
}

export default TaskList
